<?php
switch($action) {
case listGroup;
	listGroup();
	break;
case add;
	add();
	break;
case delete;
	delete();
	break;
case edit;
	edit();
	break;
case lanmuEdit;
	lanmuEdit();
	break;
}



function listGroup() {
	global $config,$userInfo;
	$Role = new Role();
	$page = intval(_g('page'))?intval(_g('page')):1;
	$num = 20;
	$sum = $Role->getRoleCount();
	$groupList = $Role->getRole($page,$num,$findkey='',$order='id',$fields='*');
	$pagination = new pagination();
	$pagination->set($sum,$num,'index.php');
	include template('manage/listGroup');
}

function add() {
	global $config,$userInfo;
	if(_g('submit')) {
		$moduleid = _g('moduleid');
		if(is_array($moduleid)) {
			$data['role'] = implode("|",$moduleid);
		}
		$group = _g('group');
		$data['roleName'] = $group['title'];
		$Role = new Role();
		$findkey[] = "roleName = '".$group['title']."'";
		$erro = $Role->getRole(1,1,$findkey,$order='id',$fields='*');
		if(!empty($erro)) {
			$message = array(
				'status' => '',
				'msg' => '角色名有冲突：请更换角色名',
			);
			echo json_encode($message);
			exit;
		}
		$data['describe'] = $group['describe'];
		$Role->sys_addRole($data);
		$message = array(
			'status' => 'success',
		);
		echo json_encode($message);
		exit;
	}else{
		$Role = new Role();
		$data = $Role->roleTreeSort();
		foreach ($data as $key=>$value){
			if($value['pid'] == 0) {
				$dataArray[] = $value;
			}
		}
		foreach ($dataArray as $key=>&$value){
			$value['Children'] = $Role->Rolefzid($value['id']);
		}
		include template('manage/addGroup');
	}
}

function delete() {
	global $config,$userInfo;
	$gid = intval(_g('gid'));
	if($userInfo['roleid'] == $gid) {
		$message = array(
			'status' => 'success',
			'msg' => '不能删除自己所在组',
		);
		echo json_encode($message);
		exit;
	}
	$Role = new Role();
	$data = $Role->sys_deltRole($gid);
	$message = array(
		'status' => 'success',
	);
	echo json_encode($message);
	exit;
}

function edit() {
	global $config,$userInfo;
	if(_g('submit')) {
		$moduleid = _g('moduleid');
		$data['role'] = implode("|",$moduleid);
		$group = _g('group');
		$id = intval(_g('id'));
		$data['roleName'] = $group['title'];
		$Role = new Role();
		$findkey[] = "roleName = '".$group['title']."'";
		$findkey[] = "id != '".$id."'";
		$erro = $Role->getRole(1,1,$findkey,$order='id',$fields='*');
		if(!empty($erro)) {
			$message = array(
				'status' => '',
				'msg' => '角色名有冲突：请更换角色名',
			);
			echo json_encode($message);
			exit;
		}
		$data['describe'] = $group['describe'];
		$data = $Role->sys_SaveEditRole($data,$id);
		$message = array(
			'status' => 'success',
		);
		echo json_encode($message);
		exit;
	}else{
		$Role = new Role();
		$gid = trim(_g('gid'));
		$group = $Role->seachRole($gid);
		$data = $Role->roleTreeSort();
		foreach ($data as $key=>$value){
			if($value['pid'] == 0) {
				$dataArray[] = $value;
			}
		}
		foreach ($dataArray as $key=>&$value){
			$value['Children'] = $Role->Rolefzid($value['id']);
		}
	}
	include template('manage/editGroup');
}

/**编辑栏目权限***/
function lanmuEdit() {
	global $config,$userInfo;
	if(_g('submit')) {
		$lanmuid = _g('moduleid');
		$data['Role'] = implode("|",$lanmuid);
		$id = intval(_g('gid'));
		$Catalog = new Catalog();
		$data = $Catalog->sys_SaveEditCatalog($data,$id);
		$message = array(
			'status' => 'success',
		);
		echo json_encode($message);
		exit;
	}else{
		
		$Role = new Role();
		$gid = trim(_g('gid'));
		$roleName = $Role->seachRole($gid);
		$Catalog = new Catalog();
		$group = $Catalog->seachUserEditRole($gid);
		$data = $Catalog->catalogTreeSort();
		foreach ($data as $key=>$value){
			if($value['pid'] == 0) {
				$dataArray[] = $value;
			}
		}
		foreach ($dataArray as $key=>&$value){
			$value['Children'] = $Catalog->Rolefzid($value['id']);
		}
	}
	include template('manage/lanmuEditGroup');
}
?>